package cz.mpelant.msoe.mealchecker;

import com.actionbarsherlock.app.SherlockActivity;

import cz.mpelant.msoe.mealchecker.downloader.Downloader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

public class LoginActivity extends SherlockActivity {
	public static final String INTENT_LOGOUT = "logout";
	private static final int DIALOG_REGISTER = 0;
	private static final String PREFERENCE_USERNAME = "username";
	private static final String PREFERENCE_PASSWORD = "password";
	private static final String REGISTER_URL="https://msoe.managemyid.com/student/register.php";
	private static final String INFO_URL="http://www.msoe.edu/life_at_msoe/current_student_resources/student_resources/idcard/managemyid.shtml";
	private static final String EMAIL_SUFFIX="@msoe.edu";
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		if (getIntent().hasExtra(INTENT_LOGOUT)) {
			logout();
		} else if (isLoginInfo(this)) {
			Intent i = new Intent(this, MainActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			finish();
		}
		setContentView(R.layout.login);
		final EditText edUsername = (EditText) findViewById(R.id.username);
		edUsername.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus && edUsername.getText().length()>0 && !edUsername.getText().toString().contains("@")){
					edUsername.setText(edUsername.getText()+EMAIL_SUFFIX);
				}
				
			}
		});
		final EditText edPassword = (EditText) findViewById(R.id.password);
		findViewById(R.id.login).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sp.edit().putString(PREFERENCE_PASSWORD, edPassword.getText().toString()).putString(PREFERENCE_USERNAME, edUsername.getText().toString()).commit();
				Intent i = new Intent(LoginActivity.this, MainActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				finish();
			}
		});
		findViewById(R.id.register).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(DIALOG_REGISTER);
			}
		});

	}

	
	@Override
	protected Dialog onCreateDialog(int id) {
		if(id==DIALOG_REGISTER){
			AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage(R.string.register_message).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(REGISTER_URL));
					startActivity(browserIntent);
				}
			}).setNeutralButton("More info", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(INFO_URL));
					startActivity(browserIntent);
					
				}
			});
			return builder.create();
		}
		return super.onCreateDialog(id);
	}
	private void logout() {
		sp.edit().remove(PREFERENCE_PASSWORD).remove(PREFERENCE_USERNAME).commit();
		Downloader.getInstance(this).cancelDownloads();
	}

	public static boolean isLoginInfo(Context ctx) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx);
		return (sp.contains(PREFERENCE_USERNAME) && sp.contains(PREFERENCE_PASSWORD));
	}
	
	public static final String getUsername(Context ctx){
		return PreferenceManager.getDefaultSharedPreferences(ctx).getString(PREFERENCE_USERNAME, null);
	}
	public static final String getPassword(Context ctx){
		return PreferenceManager.getDefaultSharedPreferences(ctx).getString(PREFERENCE_PASSWORD, null);
	}

}
